//
//  ChooseGradeViewController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChooseGradeViewController.h"
#import "GradeCollectionViewCell.h"

@interface ChooseGradeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation ChooseGradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择词汇";
    self.view.backgroundColor = rgba(229, 229, 229, 1);
    [self setUI];
    
    // Do any additional setup after loading the view.
}

- (void)setUI {
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NavigationBarHeight);
        make.height.offset(42);
    }];
    NSArray *titles = @[@"全部",@"初中",@"小学"];
    for (int i = 0; i < titles.count; i++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        titleBtn.tag = 123456789 + i;
        [titleBtn setTitle:titles[i] forState:UIControlStateNormal];
        if (i == 0) {
            [titleBtn setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
        } else {
            [titleBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
        }
        titleBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
        [titleBtn addTarget:self action:@selector(clickedTitle:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:titleBtn];
        [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(topView.mas_left).offset((96 + (SCREEN_W - 288)/2) * i);
            make.top.equalTo(topView.mas_top);
            make.height.offset(42);
            make.width.offset(96);
        }];
    }
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = rgba(12, 183, 108, 1);
    lineView.layer.cornerRadius = 1.5;
    lineView.layer.masksToBounds = YES;
    [self.view addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(NavigationBarHeight + 39);
        make.left.equalTo(self.view.mas_left).offset(34);
        make.width.offset(28);
        make.height.offset(3);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10; // 水平间距
    layout.minimumLineSpacing = 15; // 垂直间距
    layout.sectionInset = UIEdgeInsetsMake(15, 10, 15, 10); // 边界间距
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationBarHeight + 43, SCREEN_W, SCREEN_H - SafeAreaBottomHeight - NavigationBarHeight - 44) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.alwaysBounceVertical = YES;
    collectionView.pagingEnabled = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;

    [collectionView registerNib:[UINib nibWithNibName:@"GradeCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"GradeCollectionViewCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 34;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GradeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GradeCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"GradeCollectionViewCell" owner:self options:nil] lastObject];
    }
//    cell.model = self.dataArr[indexPath.row];
    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_W - 40)/3 - 1, 144);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)clickedTitle:(UIButton *)sender {
    NSInteger tag = sender.tag - 123456789;
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [self.view viewWithTag:123456789 + i];
        [btn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    }
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(NavigationBarHeight + 39);
        make.left.equalTo(self.view.mas_left).offset(34 + tag * (96 + (SCREEN_W - 288)/2));
        make.width.offset(28);
        make.height.offset(3);
    }];
    [sender setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

@end
